package com.egghiro.framework.j2se.game.core.key;

import java.util.HashMap;


public class SKeyState {
//    private static SKeyState sKeyState;
    private static final int DEFAULT_KEY_STATE = 0;
    private int simpleKeyState = DEFAULT_KEY_STATE;
    private byte[] lock = new byte[0];
    
    private static HashMap<String, SKeyState> sKeyStateMap;
    
    public static SKeyState getSKeyState(String playerId){
        if (sKeyStateMap == null) {
            synchronized (SKeyState.class) {
                if (sKeyStateMap == null) {
                    sKeyStateMap = new HashMap<String, SKeyState>();
                }
            }
        }
        SKeyState sKeyState = sKeyStateMap.get(playerId);
        if (sKeyState == null) {
            synchronized (sKeyStateMap) {
                if (sKeyStateMap.get(playerId) == null) {
                    sKeyState = new SKeyState();
                    sKeyStateMap.put(playerId, sKeyState);
                }
            }
        }
        return sKeyState;
    }
    
    protected static void removePlayer(String playerId){
        if (sKeyStateMap != null) {
            sKeyStateMap.remove(playerId);
        }
    }
    
    protected static void removeAllPlayers(){
        if (sKeyStateMap != null) {
            sKeyStateMap.clear();
        }
    }
    
    private SKeyState(){
        
    }
    
    /**
     * 上键对应的KeyState
     */
    private static final int UP_PRESSED = 1;

    /**
     * 下键对应的KeyState
     */
    private static final int DOWN_PRESSED = 2;

    /**
     * 左键对应的KeyState
     */
    private static final int LEFT_PRESSED = 4;

    /**
     * 右键对应的KeyState
     */
    private static final int RIGHT_PRESSED = 8;

    /**
     * Start对应的simpleKeyState
     */
    private static final int START_PRESSED = 16;//256;
    
    /**
     * Select对应的simpleKeyState
     */
    private static final int SELECT_PRESSED = 32;//512;
    
    /**
     * 游戏键A对应的KeyState
     */
    private static final int GAME_A_PRESSED = 64;//1024;

    /**
     * 游戏键B对应的KeyState
     */
    private static final int GAME_B_PRESSED = 128;//2048;

    /**
     * 游戏键C对应的KeyState
     */
    private static final int GAME_C_PRESSED = 256;//4096;

    /**
     * 游戏键D对应的KeyState
     */
    private static final int GAME_D_PRESSED = 512;//8192;
    
    /**
     * 游戏键L1对应的KeyState
     */
    private final static int GAME_L1_PRESSED = 1024;
    
    /**
     * 游戏键R1对应的KeyState
     */
    private final static int GAME_R1_PRESSED = 2048;
    
    /**
     * 游戏键L2对应的KeyState
     */
    private final static int GAME_L2_PRESSED = 4096;
    
    /**
     * 游戏键R2对应的KeyState
     */
    private final static int GAME_R2_PRESSED = 8192;
    
    public int getSimpleKeyState() {
        return simpleKeyState;
    }

//    public static SKeyState getSKeyState() {
//        if (sKeyState == null) {
//            synchronized (lock) {
//                if (sKeyState == null) {
//                    sKeyState = new SKeyState();
//                }
//            }
//        }
//        return sKeyState;
//    }
    
    

    /**
     * 上键是否被按下
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyUpPressed(int simpleKeyState) {
        return (simpleKeyState & UP_PRESSED) != 0;
    }

    public boolean isKeyUpPressed() {
        return isKeyUpPressed(simpleKeyState);
    }

    /**
     * 下键是否被按下
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyDownPressed(int simpleKeyState) {
        return (simpleKeyState & DOWN_PRESSED) != 0;
    }

    public boolean isKeyDownPressed() {
        return isKeyDownPressed(simpleKeyState);
    }

    /**
     * 左键是否被按下
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyLeftPressed(int simpleKeyState) {
        return (simpleKeyState & LEFT_PRESSED) != 0;
    }

    public boolean isKeyLeftPressed() {
        return isKeyLeftPressed(simpleKeyState);
    }

    /**
     * 右键是否被按下
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyRightPressed(int simpleKeyState) {
        return (simpleKeyState & RIGHT_PRESSED) != 0;
    }

    public boolean isKeyRightPressed() {
        return isKeyRightPressed(simpleKeyState);
    }

    /**
     * Start是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyStartPressed(int simpleKeyState) {
        return (simpleKeyState & START_PRESSED) != 0;
    }

    public boolean isKeyStartPressed() {
        return isKeyStartPressed(simpleKeyState);
    }
    
    /**
     * Select是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeySelectPressed(int simpleKeyState) {
        return (simpleKeyState & SELECT_PRESSED) != 0;
    }

    public boolean isKeySelectPressed() {
        return isKeySelectPressed(simpleKeyState);
    }

    /**
     * GameA是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameAPressed(int simpleKeyState) {
        return (simpleKeyState & GAME_A_PRESSED) != 0;
    }

    public boolean isKeyGameAPressed() {
        return isKeyGameAPressed(simpleKeyState);
    }

    /**
     * GameB是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameBPressed(int simpleKeyState) {
        return (simpleKeyState & GAME_B_PRESSED) != 0;
    }

    public boolean isKeyGameBPressed() {
        return isKeyGameBPressed(simpleKeyState);
    }

    /**
     * Game C是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameCPressed(int simpleKeyState) {
        return (simpleKeyState & GAME_C_PRESSED) != 0;
    }

    public boolean isKeyGameCPressed() {
        return isKeyGameCPressed(simpleKeyState);
    }

    /**
     * GameD是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameDPressed(int simpleKeyState) {
        return (simpleKeyState & GAME_D_PRESSED) != 0;
    }

    public boolean isKeyGameDPressed() {
        return isKeyGameDPressed(simpleKeyState);
    }

    /**
     * L1是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameL1Pressed(int simpleKeyState) {
        return (simpleKeyState & GAME_L1_PRESSED) != 0;
    }

    public boolean isKeyGameL1Pressed() {
        return isKeyGameL1Pressed(simpleKeyState);
    }
    
    /**
     * R1是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameR1Pressed(int simpleKeyState) {
        return (simpleKeyState & GAME_R1_PRESSED) != 0;
    }

    public boolean isKeyGameR1Pressed() {
        return isKeyGameR1Pressed(simpleKeyState);
    }
    
    /**
     * L2是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public static boolean isKeyGameL2Pressed(int simpleKeyState) {
        return (simpleKeyState & GAME_L2_PRESSED) != 0;
    }

    public boolean isKeyGameL2Pressed() {
        return isKeyGameL2Pressed(simpleKeyState);
    }
    
    /**
     * R2是否被按下了
     * 
     * @param simpleKeyState
     * @return
     */
    public boolean isKeyGameR2Pressed(int simpleKeyState) {
        return (simpleKeyState & GAME_R2_PRESSED) != 0;
    }

    public boolean isKeyGameR2Pressed() {
        return isKeyGameR2Pressed(simpleKeyState);
    }
    
    public boolean pressKeyUp(){
        if(isKeyUpPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | UP_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyUp(){
        if (isKeyUpPressed()) {
            synchronized (lock) {
//                if (isKeyUpPressed()) {
                simpleKeyState = simpleKeyState ^ UP_PRESSED;
//                }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyDown(){
        if(isKeyDownPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | DOWN_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyDown(){
        if (isKeyDownPressed()) {
            synchronized (lock) {
    //            if (isKeyDownPressed()) {
                simpleKeyState = simpleKeyState ^ DOWN_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyLeft(){
        if(isKeyLeftPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | LEFT_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyLeft(){
        if (isKeyLeftPressed()) {
            synchronized (lock) {
//                if (isKeyLeftPressed()) {
                simpleKeyState = simpleKeyState ^ LEFT_PRESSED;
//                }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyRight(){
        if(isKeyRightPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | RIGHT_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyRight(){
        if (isKeyRightPressed()) {
            synchronized (lock) {
    //            if (isKeyRightPressed()) {
                simpleKeyState = simpleKeyState ^ RIGHT_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyStart(){
        if(isKeyStartPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | START_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyStart(){
        if (isKeyStartPressed()) {
            synchronized (lock) {
    //            if (isKeyStartPressed()) {
                simpleKeyState = simpleKeyState ^ START_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeySelect(){
        if (isKeySelectPressed() == false) {
            synchronized (lock) {
                simpleKeyState = simpleKeyState | SELECT_PRESSED;
            }
        }
        return false;
    }
    
    public boolean releaseKeySelect(){
        if (isKeySelectPressed()) {
            synchronized (lock) {
    //            if (isKeySelectPressed()) {
                simpleKeyState = simpleKeyState ^ SELECT_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameA(){
        if(isKeyGameAPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_A_PRESSED;
            }
        }
        return false;
    }
    
    public boolean releaseKeyGameA(){
        if (isKeyGameAPressed()) {
            synchronized (lock) {
    //            if (isKeyGameAPressed()) {
                    simpleKeyState = simpleKeyState ^ GAME_A_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameB(){
        if(isKeyGameBPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_B_PRESSED;
            }
        }
        return false;
    }
    
    public boolean releaseKeyGameB(){
        if (isKeyGameBPressed()) {
            synchronized (lock) {
    //            if (isKeyGameBPressed()) {
                simpleKeyState = simpleKeyState ^ GAME_B_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameC(){
        if(isKeyGameCPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_C_PRESSED;
            }
        }
        return false;
    }
    
    public boolean releaseKeyGameC(){
        if (isKeyGameCPressed()) {
            synchronized (lock) {
    //            if (isKeyGameCPressed()) {
                simpleKeyState = simpleKeyState ^ GAME_C_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameD(){
        if(isKeyGameDPressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_D_PRESSED;
            }
        }
        return false;
    }
    
    public boolean releaseKeyGameD(){
        if (isKeyGameDPressed()) {
            synchronized (lock) {
    //            if (isKeyGameDPressed()) {
                simpleKeyState = simpleKeyState ^ GAME_D_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameL1(){
        if(isKeyGameL1Pressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_L1_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyGameL1(){
        if (isKeyGameL1Pressed()) {
            synchronized (lock) {
    //            if (isKeyGameL1Pressed()) {
                simpleKeyState = simpleKeyState ^ GAME_L1_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameR1(){
        if(isKeyGameR1Pressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_R1_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyGameR1(){
        if (isKeyGameR1Pressed()) {
            synchronized (lock) {
    //            if (isKeyGameR1Pressed()) {
                simpleKeyState = simpleKeyState ^ GAME_R1_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameL2(){
        if(isKeyGameL2Pressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_L2_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyGamel2(){
        if (isKeyGameL2Pressed()) {
            synchronized (lock) {
    //            if (isKeyGameL2Pressed()) {
                simpleKeyState = simpleKeyState ^ GAME_L2_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean pressKeyGameR2(){
        if(isKeyGameR2Pressed() == false){
            synchronized (lock) {
                simpleKeyState = simpleKeyState | GAME_R2_PRESSED;
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseKeyGameR2(){
        if (isKeyGameR2Pressed()) {
            synchronized (lock) {
    //            if (isKeyGameR2Pressed()) {
                simpleKeyState = simpleKeyState ^ GAME_R2_PRESSED;
    //            }
            }
            return true;
        }
        return false;
    }
    
    public boolean releaseAll(){
        synchronized (lock) {
            simpleKeyState = DEFAULT_KEY_STATE;
        }
        return true;
    }
}
